/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VLSObjects;


import DBConnection.*;
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class TransactionManager {
    
    public boolean addTransaction(Transaction newTransaction)
    {
        boolean result = false;
        String transactionID;
        
         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;
            
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO transaction (transaction_date, transaction_time, plate_num) VALUES(?,?,?)");
            
            pstmt.setString(i++, now("yyyy-MM-dd"));
            pstmt.setString(i++, now("hh:mm:ss"));
            pstmt.setString(i++, newTransaction.getPlateNumber());

            //execute query
            pstmt.executeUpdate();
            
            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean editTransaction(Transaction editTransaction)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
           PreparedStatement pstmt = conn.prepareStatement("UPDATE transaction SET plate_num = ? WHERE transaction_id = ?" );

            //include parameters
            pstmt.setString(i++, editTransaction.getPlateNumber());

            pstmt.setString(i++, editTransaction.getTransactionID());
            

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public String getLastTransactionID()
    {   String lastTransactionID = "null";
    
        boolean result;
        
         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;
            
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("SELECT MAX(transaction_id) FROM transaction ORDER BY transaction_id");
            
            //execute query
            ResultSet rs = pstmt.executeQuery();
            
            while(rs.next())
            {
                result = true;
                lastTransactionID = rs.getString(1);
            }
            
            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
         
         
    
        return lastTransactionID;
        
    }
    
    public static String now(String dateFormat) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(cal.getTime());
    }
    
}
